<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>类别列表</span>
        <el-button type="text" @click="addDialog = true"> 添加分类 </el-button>
      </div>
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="obj">
            <el-button size="mini" @click="showEditDiglog(obj.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加的弹窗 -->
    <el-dialog title="添加分类" :visible.sync="addDialog" width="500px" :before-close="cancel">
      <!-- 表单 -->
      <el-form label-width="80px" :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑的对话框 -->
    <el-dialog title="修改分类" :visible.sync="editDialog" width="500px" close-on-click-modal>
      <!-- 表单 -->
      <el-form label-width="80px" :model="editData" :rules="rules" ref="editForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryAPI,
  addCategoryAPI,
  deleteCategoryAPI,
  updateCategoryAPI
} from '@/api/category'
export default {
  data() {
    return {
      list: [],
      // 控制添加弹窗数据项
      addDialog: false, // false 表示隐藏对话框, true表示显示对话框
      // 控制编辑弹窗的数据
      editDialog: false,

      // 点击添加表单的数据
      addData: {
        cate_name: '',
        cate_alias: ''
      },

      // 收集表单的数据
      editData: {},

      // 验证对象
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1~10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类名称必须是1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async getCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    // 添加分类
    addCategory() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message)
          // 重置表单
          this.$refs.addForm.resetFields()
          // 关闭弹框
          this.addDialog = false
          // 重新获取分类列表数据
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel() {
      this.$refs.addForm.resetFields()
      this.addDialog = false
    },
    // 删除的方法
    del(id) {
      // console.log(id)
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除id=1和id=2的数据')
      // 询问是否确认要删除
      this.$confirm('确定要删除吗？', '提示', {
        callback: async r => {
          // console.log(r) // cancel confirm
          if (r === 'confirm') {
            const { data: res } = await deleteCategoryAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              // 重新加载文章分类列表
              this.getCategory()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 显示对话框
    showEditDiglog(row) {
      // console.log(row) // row 就是正在编辑的这条数据
      this.editData = { ...row }
      this.editDialog = true
    },

    // 完成部分更新  修改方法
    updateCategory() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCategoryAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.getCategory() // 重新获取数据，重新更新页面
          this.editDialog = false // 关闭弹出层
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },
  created() {
    this.getCategory()
  }
}
</script>

<style>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
